﻿using Comm;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kinlo.Repo
{
    public class StatusRep
    {
        string strConn;
        public StatusRep()
        {
            strConn = System.Configuration.ConfigurationManager.ConnectionStrings["WeightDB"].ConnectionString;

        }
        public int AddMachineStatus(string equipCode, int status)
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                {
                    var cmd = string.Format(@"INSERT dbo.MachineStatus( EquipCode, Status, UploadTime )
                                            VALUES  ( N'{0}',N'{1}',GETDATE())", equipCode, status);
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("{0} {1} {2}", "StatusRep", "AddMachineStatus", ex.Message)); 
                return 0;
            }
        }

        public int AddAlarmStatus(string equipCode, string alarmCode, int status)
        {
            try
            {
                using (var conn = new SqlConnection(strConn))
                {
                    var cmd = string.Format(@"INSERT dbo.AlarmStatus( EquipCode ,AlarmCode ,AlarmStatus ,UploadTime)
                                            VALUES( '{0}','{1}','{2}', GETDATE())", equipCode, alarmCode, status);
                    var sqlCmd = new SqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("{0} {1} {2}", "StatusRep", "AddAlarmStatus", ex.Message)); 
                return 0;
            }
        }
    }
}
